# coding=utf-8
from . import base
from .component import Component


## 实现一个组件，需要写如下几个方法：
class Docker(Component):

    ## 依赖的组件，返回类型为 string 数组，例如 return ["docker"]，表示该组件依赖的其他组件
    def dependencies(self):
        if base.is_windows():
            return []
        else:
            return []



    ## 检测安装状态，返回true或者false，用于显示是否已经安装该组件
    def check(self):
        ret = base.cmd("docker", ["-v"], True)
        if ret == 0:
            return True;
        else:
            return False;



    ## 检测运行状态，返回true或者false，表示该组件是否正在运行
    def status(self):
        if base.is_windows():
            ## 查询是否存在 docker desktop程序
            return base.is_program_running("Docker Desktop.exe")
        else:
            content = base.cmd_with_content("systemctl", ["is-active", "docker"]);
            if content == 'active':
                return True
            return False




    ## 安装组件 
    def install(self, rVersion : str = None):
        EN_PATH = base.get_env("EN_PATH")
        if base.is_windows():
            base.cmd_in_dir(f"{EN_PATH}/windows/docker", "./install_docker.bat")
        else:
            # 如果是麒麟系统，需要特殊处理
            if base.is_kylin():
                base.cmd_in_dir(f"{EN_PATH}/linux/docker", "./install_docker_kl.sh")

                return;
            base.cmd_in_dir(f"{EN_PATH}/linux/docker", "./install_docker.sh")
            if base.is_apt():
                base.cmd("apt", ["install", "docker-compose", "-y"])
            else:
                base.cmd("yum", ["install", "docker-compose", "-y"])
        return True;

    ## 更新组件
    def update(self, rVersion : str = None):

        ## 因为install 和 update内容相同，所以直接调用
        self.install();


    ## 卸载
    def uninstall(self):
        EN_PATH = base.get_env("EN_PATH")
        if base.is_windows():
            base.cmd_in_dir(f"{EN_PATH}/windows/docker", "./uninstall_docker.bat")
        else:
            base.cmd_in_dir(f"{EN_PATH}/linux/docker", "./uninstall_docker.sh")


from . import component_manager as cm
cm.ALL_COMPONENTS['docker'] = Docker()